package cn.line5.seam.example;

import java.util.List;

import cn.line5.seam.annotations.Name;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.db.Query;
import cn.line5.seam.db.QueryFactory;

@Name("testDao")
/**
 * <h1>LSEAM框架示例数据库实现类</h1>
 * <br/>
 * 
 * @author Liu,Xiaojun
 */
public class TestDaoImpl implements TestDao {
	public List<TestVo> getList() {
		Query query = QueryFactory
				.createQuery("select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user");
		List<TestVo> l = query.list(TestVo.class);
		return l;
	}

	public PageResult<TestVo> getPageList(String currentPage) {
		Integer page = Integer.parseInt(currentPage == null
				|| currentPage.length() <= 0 ? "1" : currentPage);
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user")
				.setCurrentPage(
						page > 0 ? page : 1).setPageRows(3);
		PageResult<TestVo> pr = query.pageList(TestVo.class);
		return pr;
	}

	public TestVo getUser(String id) {
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user where user_id=?")
				.setParameter(1, id);
		return query.single(TestVo.class);
	}

	public boolean insertUser(TestVo vo) {
		Query query = QueryFactory
				.createExecute(
						"insert into t_user values(:userId, :loginName, :loginPass, :userName, :userPhoto)",
						vo);
		return query.execute() > 0;
	}

	public boolean modifyUser(TestVo vo) {
		Query query = QueryFactory
				.createExecute(
						"update t_user set login_name=:loginName, login_pass=:loginPass, user_name=:userName, user_photo=:userPhoto where user_id=:userId",
						vo);
		return query.execute() > 0;
	}

	public boolean deleteUser(String id) {
		Query query = QueryFactory.createExecute(
				"delete from t_user where user_id=?").setParameter(1, id);
		return query.execute() > 0;
	}
}
